from property import Property

class IntegerProperty(Property):
    ''' Used to represent integer values.
    
        Indexed as ``int``.
    '''
    def __init__(self, **kwargs):
        kwargs['type'] = long
        super(IntegerProperty, self).__init__(**kwargs)
    
    def get_index_types(self):
        return ['int']
        
    def get_index_keys(self):
        return ['%s_int' % self.get_id()]
        
    def encode_index_values(self, v):
        return [v]   
        
    def encode_single(self, v):
        if not isinstance(v, (int, long)):
            raise ValueError("IntegerProperty property %s value must be a int. Got %s" % (self.get_id(), type(v)))
        return v
        
    def encode_repeated(self, v):
        l = []
        for i, val in enumerate(v):
            if not isinstance(val, (int, long)):
                raise ValueError("IntegerProperty property %s value must be a list of int. Got %s at %s" % (self.get_id(), type(val), i))
        return v
        
    def encode_other(self, other):
        if isinstance(other, (int, long)):
            return other
        else:
            raise ValueError("Filter %s value must be an int. Got %s" % (self.get_id(), type(other)))
        
    def decode_single(self, v):
        return v
        
    def decode_repeated(self, v):
        return v
